package managers.messengerManager.services;

import java.io.File;
import java.util.ResourceBundle;

import managers.messengerManager.FeedbackManager;
import managers.messengerManager.MessengerManager;

import utils.ManagingMessage;
import utils.ManagingMessage.Type;
import utils.StackTracePrinter;

public class SendSupportDocumentService extends Service {


	private File document;
	private int baseWaitTime;


	public SendSupportDocumentService(File document) {
		this(document, DEFAULT_BASE_WAIT_TIME);
	}

	public SendSupportDocumentService(File document, int baseWaitTime) {
		super("sendSupportDocument");
		this.document = document;
		this.baseWaitTime = baseWaitTime;
	}


	public void run() {
		sendDocument();
	};


	private synchronized void sendDocument () {
		ManagingMessage message = new ManagingMessage (Type.SUPPORT_DOCUMENT);
		message.putProperty("document", document);
		try {
			MessengerManager.getSharedInstance().getSingleProtocol().send(message,
					MessengerManager.getSharedInstance().getServerAddress(), MessengerManager.getSharedInstance().getServerPort());

			wait(baseWaitTime*2);
			if (externalObject != null) {
				MessengerManager.getSharedInstance().getFeedbackManager().updateStatus(ResourceBundle.getBundle("managers.messengerManager.services.messages").getString("m.9"), FeedbackManager.DEFAULT);
			} else {
				MessengerManager.getSharedInstance().getFeedbackManager().updateStatus(ResourceBundle.getBundle("managers.messengerManager.services.messages").getString("m.10"), FeedbackManager.WARNING);
			}
		} catch (Exception e) {
			StackTracePrinter.print(e);
			MessengerManager.getSharedInstance().getFeedbackManager().updateStatus(ResourceBundle.getBundle("managers.messengerManager.services.messages").getString("m.10"), FeedbackManager.WARNING);
		}
	}

}
